package com.dearbinge.opentapi.content.rocket;

import com.dearbinge.data.api.rocket.RocketService;
import com.dearbinge.data.pojo.rocket.Rocket;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.date.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(description = "火箭")
@RestController
@RequestMapping("/rocket")
@EnableAutoConfiguration
public class RocketController extends WebMvcConfigurerAdapter {

    @Autowired
    private RocketService rocketService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody Rocket rocket){
        try {
            if (rocket.getId() != null && rocket.getId() != 0){
                Integer dataCode = rocketService.update(rocket);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = rocketService.insert(rocket);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID与类型查询
     */
    @ApiOperation(value = "根据用户ID查询",notes = "根据用户ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "rocketType",value = "类型（0：充值   1：派送）",paramType = "query")})
    @RequestMapping(value = "/getByUserId",method = RequestMethod.GET)
    public ResultMsg<Map<String,Object>> getByUserId(String userId,Integer rocketType){
        try {
            Map<String,Object> rocket = rocketService.getByUserId(userId,rocketType);
            return ResultMsg.ok(rocket);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除",notes = "根据ID删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = rocketService.delete(id);
            if (dataCode > 0){
                ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据条件分页查询
     */
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query"),
            @ApiImplicitParam(name = "rocketType",value = "类型（0：充值   1：派送）",paramType = "query")})
    @RequestMapping(value = "getPage",method = RequestMethod.GET)
    public PageResult<Rocket> getPage(Integer pageNo, Integer pageSize, String userId, Integer rocketType){
        PageResult pageResult = new PageResult();
        try {
            pageResult = rocketService.getPage(pageNo,pageSize,userId,rocketType);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }

    /**
     * 根据用户ID点击
     */
    @ApiOperation(value = "根据用户ID点击",notes = "根据用户ID点击")
    @ApiImplicitParam(name = "userId",value = "用户ID",paramType = "query")
    @RequestMapping(value = "/toClick",method = RequestMethod.GET)
    public ResultMsg<String> toClick(String userId){
        try {
            List<Rocket> rocketList = rocketService.getById(userId);
            if(rocketList != null && rocketList.get(0).getLastClickTime() != null){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String endTime = DateUtils.date2String(new Date());
                    String staTime = DateUtils.date2String(rocketList.get(0).getLastClickTime());
                    int ilastClickTime = DateUtils.compareTime(staTime, endTime, 1);
                /*long diff;
                diff = sd.parse(endTime).getTime() - sd.parse(staTime).getTime();
                long day=diff/(24*60*60*1000);
                long hour=(diff/(60*60*1000)-day*24);
                long min = ((diff/(60*1000))-day*24*60-hour*60);//计算差多少分钟
                long hour2 = 2 - hour;
                long min2 = 60 - min;*/
                /*if (ilastClickTime < 2){
                        return ResultMsg.error(500,"对不起，两个小时内只能点击一次，请于稍后再点击！");
                }*/
            }
            Integer countNumber = rocketService.getNumber(userId);
            if (countNumber == 0){
                return ResultMsg.error(500,"火箭次数已用完，请充值后使用！");
            }
            Integer dataCode = rocketService.toClick(userId);
            if (dataCode > 0){
                return ResultMsg.ok("置顶成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 火箭充值接口
     */
    @ApiOperation(value = "火箭充值",notes = "火箭充值")
    @RequestMapping(value = "/recharge ",method = RequestMethod.POST)
    public ResultMsg<String> recharge(@RequestBody Rocket rocket){
        try {
            Integer dataCode = rocketService.recharge(rocket);
            if (dataCode > 0){
                return ResultMsg.ok("充值成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 定时每天晚上凌晨00:00跑批
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    public void timerNumber(){
        System.out.print("================================00:00执行批量操作======================================");
        rocketService.timerNumber();
    }

}
